﻿// ExportFormatDialog.xaml.cs
using StudentInfoSystem.Core.Interfaces;
using System;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace StudentInfoSystem.UI.Views.Dialogs
{
    public partial class ExportFormatDialog : Window
    {
        public ExportFormat? SelectedFormat { get; private set; }

        public ExportFormatDialog()
        {
            InitializeComponent();

            // 确保窗口出现在屏幕中央
            WindowStartupLocation = WindowStartupLocation.CenterOwner;
            Loaded += (s, e) => {
                // 设置初始焦点
                CancelButton.Focus();

                // 设置自定义图标
                SetWindowIcon();
            };
        }

        private void SetWindowIcon()
        {
            try
            {
                // 使用内置的应用程序图标
                Icon = Application.Current.MainWindow?.Icon;

                // 如果没有图标，创建一个简单的图标
                if (Icon == null)
                {
                    var drawing = new GeometryDrawing(
                        Brushes.Transparent,
                        null,
                        null);

                    var drawingGroup = new DrawingGroup();
                    drawingGroup.Children.Add(drawing);

                    var image = new DrawingImage(drawingGroup);
                    Icon = image;
                }
            }
            catch
            {
                // 如果无法设置图标，忽略错误
            }
        }

        private void SelectFormat(ExportFormat format)
        {
            SelectedFormat = format;
            DialogResult = true;
            Close();
        }

        private void SQLiteButton_Click(object sender, RoutedEventArgs e) => SelectFormat(ExportFormat.SQLite);
        private void SQLServerButton_Click(object sender, RoutedEventArgs e) => SelectFormat(ExportFormat.SQLServer);
        private void MySQLButton_Click(object sender, RoutedEventArgs e) => SelectFormat(ExportFormat.MySQL);
        private void CSVButton_Click(object sender, RoutedEventArgs e) => SelectFormat(ExportFormat.CSV);
        private void JSONButton_Click(object sender, RoutedEventArgs e) => SelectFormat(ExportFormat.JSON);

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            DialogResult = false;
            Close();
        }

        // 添加窗口拖动功能
        private void TitleBar_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (e.ChangedButton == MouseButton.Left)
                DragMove();
        }
    }
}